package br.com.sistema.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry;

public class StringUtils {

    static String[] conjuncoesArray = {"di", "de", "da", "do", "dos", "das", "a", "o", "e", "os", "as", ")", "(", ",", "\"", "\'", "\\", "/", "em", "no", "na", "nos", "nas", "&"};

    public static String primeriaMaiuscula(String palavra) {
        if (palavra != null && !palavra.equalsIgnoreCase("")) {
            int len = palavra.length();
            String out = "";
            out += palavra.substring(0, 1).toUpperCase();
            out += palavra.substring(1, len);
            return out;
        }
        return palavra;
    }

    public static String truncaLength(String aTruncar, int maxLength) {
        if (aTruncar.length() > maxLength) {
            return aTruncar.substring(0, maxLength);
        } else {
            return aTruncar;
        }
    }

    public static String trataCasaDecimal(Double valor) {
        String tx = valor.toString();
        tx = tx.replace('.', ',');
        if (tx.contains(",")) {
            if (tx.split(",")[1].length() == 1) {
                tx += "0";
            } else if (tx.split(",")[1].length() == 0) {
                tx += "00";
            }
        }
        tx = tx.replace(',', '.');
        return tx;
    }

    public static String[] removePreposicoes(String[] str) {

        ArrayList<String> conjuncoes = toArrayList(conjuncoesArray);
        ArrayList<String> palavras = toArrayList(str);

        for (String conjuncao : conjuncoes) {
            if (palavras.contains(conjuncao)) {
                palavras.remove(conjuncao);
            }
        }

        return toArray(palavras);
    }

    public static String[] minusculo(String[] str) {
        for (int i = 0; i < str.length; i++) {
            str[i] = str[i].toLowerCase();
        }
        return str;
    }

    public static ArrayList<String> toArrayList(String[] str) {
        ArrayList<String> retorno = new ArrayList<String>();
        retorno.addAll(Arrays.asList(str));
        return retorno;
    }

    public static String[] toArray(ArrayList<String> str) {
        String[] retorno = new String[str.size()];
        for (int i = 0; i < str.size(); i++) {
            retorno[i] = str.get(i);
        }
        return retorno;
    }

    public static String[] quebrarEFormatarString(String texto) {
        String[] textoFormatado = texto.toLowerCase().split(" ");
        textoFormatado = removePreposicoes(textoFormatado);
        return textoFormatado;
    }

    public static String getRandomString() {
        return getRandomString(6, 6);
    }

    public static String getRandomString(int lo, int hi) {
        int n = rand(lo, hi);
        byte b[] = new byte[n];
        for (int i = 0; i < n; i++) {
            b[i] = (byte) rand('a', 'z');
        }
        return new String(b, 0);
    }

    private static int rand(int lo, int hi) {
        java.util.Random rn = new java.util.Random();
        int n = hi - lo + 1;
        int i = rn.nextInt(n);
        if (i < 0) {
            i = -i;
        }
        return lo + i;
    }
}
